blog

Home / DeveloperSection / Blogs / Action Selector in Asp.net MVC

Action Selector in Asp.net MVC

Manish Kumar2859 25-Feb-2017

Action Selector are the attributes that are applied to the action methods .Action selector helps in the routing engine to select particular action method to handle a particular user request. In this article I will explain two of them action name and Non action .Action verb in the next article. 

1-Action Name            

2-Non Action

3-Action Verb 

Action Name

Action Name is the attribute that represent the action name. let us take an example to understand action name.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
 
namespace DemoStoreproc.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
    
        publicActionResult Index()
        {
            var context = newDemoEntities();
            ViewBag.Country =context.CountryLists;
            ViewBag.users = context.RegistrationForms.ToList();
 
            return View();
        }
 
        publicstring Gettime()
        {
            returnDateTime.Now.ToString();
        }
}
}

 

Now build and run the application the index page will run


type this  in the url to run time



http://localhost:60305/Home/Gettime

 

 

you will this output


Action Selector in Asp.net MVC

Now add action name attribute in the controller

                            

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
 
namespace DemoStoreproc.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
    
        publicActionResult Index()
        {
            var context = newDemoEntities();
            ViewBag.Country = context.CountryLists;
            ViewBag.users = context.RegistrationForms.ToList();
 
            return View();
        }
        [ActionName("CurrentTime")]
        publicstring Gettime()
        {
            returnDateTime.Now.ToString();
        }
    }
}

 

Again run the application and type http://localhost:60305/Home/Gettime


in the url you will get this error beacause I have applied action name with


CurrentTime.


Action Selector in Asp.net MVC



Now add action name attribute in the controller

                            

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
 
namespace DemoStoreproc.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
    
        publicActionResult Index()
        {
            var context = newDemoEntities();
            ViewBag.Country = context.CountryLists;
            ViewBag.users = context.RegistrationForms.ToList();
 
            return View();
        }
        [ActionName("CurrentTime")]
        publicstring Gettime()
        {
            returnDateTime.Now.ToString();
        }
    }
}

 

Again run the application and type http://localhost:60305/Home/Gettime


in the url you will get this error beacause I have applied action name with


CurrentTime.


Action Selector in Asp.net MVC



Now change the


http://localhost:60305/Home/CurrentTime


Action Selector in Asp.net MVC




Non Action

Non action attribute that is use in  a controller when we want that a public method of controller that it should not be treated as action method.                                

Now I will take an example to explain Non action . This example will make clear the use of Non action

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DemoStoreproc.Models;
using System.Data;
using System.Data.Entity;
 
namespace DemoStoreproc.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
    
        publicActionResult Index()
        {
            var context = newDemoEntities();
            ViewBag.Country = context.CountryLists;
            ViewBag.users = context.RegistrationForms.ToList();
 
            return View();
        }
        [ActionName("CurrentTime")]
        publicstring Gettime()
        {
            return Time();
        }
        [NonAction]
        publicstring Time()
        {
            returnDateTime.Now.ToString();
        }
    }
}

 

In the above example Time action cannot be use in the url beacause it has the attribute with NonAction so it will not be treated as action method.


Updated 17-Mar-2018

Leave Comment

Comments

Liked By